package org.example.manage;

import org.example.model.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserManage {
    private Connection connection;
    private PreparedStatement statement;
    public UserManage() {
        try {
            Class.forName("org.h2.Driver");
            connection = DriverManager.getConnection
                    ("jdbc:h2:./data/milk_data;DB_CLOSE_DELAY=-1");
        } catch (SQLException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public void updateUser(String target, String object){
        String sql = String.format("update USERS set %s where %s", target, object);
        try {
            statement = connection.prepareStatement(sql);
            statement.executeUpdate();
            statement.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public void create(){
        String tableSql = "create table if not exists USERS (name varchar(50)," +
                    "mailbox varchar(50), password varchar(50), " +
                    "avatarPath varchar(50), signature varchar(50), " +
                    "orderPath varchar(50), identity varchar(50), condition varchar(50)); ";
        try {
            statement = connection.prepareStatement(tableSql);
            statement.executeUpdate();
            statement.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public void insertUser(User user){
        try {
            statement = connection.prepareStatement("insert into USERS values (?,?,?,?,?,?,?,?)");
            statement.setString(1, user.name);
            statement.setString(2, user.mailbox);
            statement.setString(3, user.password);
            statement.setString(4, user.avatarPath);
            statement.setString(5, user.signature);
            statement.setString(6, user.orderPath);
            statement.setString(7, user.identity);
            statement.setString(8, user.condition);
            statement.executeUpdate();
            statement.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public void deleteUser(String target){
        String sql = String.format("delete USERS where '%s'", target);
        try {
            statement = connection.prepareStatement(sql);
            statement.executeUpdate();
            statement.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public User selectUser(String mailbox){
        User user = new User();
        try {
            String sql = String.format("select * from USERS where mailbox='%s'", mailbox);
            statement = connection.prepareStatement(sql);
            ResultSet set = statement.executeQuery();
            if(set.next()) {
                user.name = set.getString(1);
                user.mailbox = set.getString(2);
                user.password = set.getString(3);
                user.avatarPath = set.getString(4);
                user.signature = set.getString(5);
                user.orderPath = set.getString(6);
                user.identity = set.getString(7);
                user.condition = set.getString(8);
            }
            set.close();
            statement.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return user;
    }
    public List<User> selectAll() {
        List<User> list = new ArrayList<>();
        try {
            String sql = "select * from USERS";
            statement = connection.prepareStatement(sql);
            ResultSet set = statement.executeQuery();
            while(set.next()) {
                User user = new User();
                user.name = set.getString(1);
                user.mailbox = set.getString(2);
                user.password = set.getString(3);
                user.avatarPath = set.getString(4);
                user.signature = set.getString(5);
                user.orderPath = set.getString(6);
                user.identity = set.getString(7);
                user.condition = set.getString(8);
                list.add(user);
            }
            set.close();
            statement.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }
    public void close(){
        try {
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public boolean hasUser(String mailbox){
        boolean has = true;
        String sql = String.format("select * from USERS where mailbox='%s'", mailbox);
        try {
            statement = connection.prepareStatement(sql);
            ResultSet set = statement.executeQuery();
            has = set.next();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return has;
    }
}
